package org.dromara.netbar.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.netbar.domain.bo.NetbarCouponMemberBo;
import org.dromara.netbar.service.INetbarCouponMemberService;
import org.dromara.netbar.service.INetbarGiftPackItemService;
import org.dromara.netbar.service.INetbarGiftPackService;
import org.dromara.netbar.service.impl.CouponService;
import org.dromara.netbar.service.impl.MemberService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.netbar.domain.vo.NetbarCouponVo;
import org.dromara.netbar.domain.bo.NetbarCouponBo;
import org.dromara.netbar.service.INetbarCouponService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 通用卡券
 *
 * @author Lion Li
 * @date 2025-07-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/netbar/coupon")
public class NetbarCouponController extends BaseController {

    private final INetbarCouponService netbarCouponService;
    private final INetbarCouponMemberService netbarCouponMemberService;
    private final INetbarGiftPackItemService netbarGiftPackItemService;
    private final CouponService couponService;
    private final MemberService memberService;

    /**
     * 查询通用卡券列表
     */
    @SaCheckPermission("netbar:coupon:list")
    @GetMapping("/list")
    public TableDataInfo<NetbarCouponVo> list(NetbarCouponBo bo, PageQuery pageQuery) {
        return netbarCouponService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出通用卡券列表
     */
    @SaCheckPermission("netbar:coupon:export")
    @Log(title = "通用卡券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NetbarCouponBo bo, HttpServletResponse response) {
        List<NetbarCouponVo> list = netbarCouponService.queryList(bo);
        ExcelUtil.exportExcel(list, "通用卡券", NetbarCouponVo.class, response);
    }

    /**
     * 获取通用卡券详细信息
     *
     * @param couponId 主键
     */
    @SaCheckPermission("netbar:coupon:query")
    @GetMapping("/{couponId}")
    public R<NetbarCouponVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long couponId) {
        return R.ok(netbarCouponService.queryById(couponId));
    }

    /**
     * 新增通用卡券
     */
    @SaCheckPermission("netbar:coupon:add")
    @Log(title = "通用卡券", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NetbarCouponBo bo) {
        return toAjax(netbarCouponService.insertByBo(bo));
    }

    /**
     * 修改通用卡券
     */
    @SaCheckPermission("netbar:coupon:edit")
    @Log(title = "通用卡券", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NetbarCouponBo bo) {
        return toAjax(netbarCouponService.updateByBo(bo));
    }

    /**
     * 删除通用卡券
     *
     * @param couponIds 主键串
     */
    @SaCheckPermission("netbar:coupon:remove")
    @Log(title = "通用卡券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] couponIds) {
        Map<Long,Integer> couponMemberCountMap = netbarCouponMemberService.countMapByCouponIds(couponIds);
        if(null != couponMemberCountMap && !couponMemberCountMap.keySet().isEmpty()){
            return R.fail("有会员拥有此通用卡券，无法删除");
        }
        Map<Long,Integer> packItemCountMap = netbarGiftPackItemService.countMapByCouponIds(couponIds);
        if(null !=packItemCountMap && !packItemCountMap.keySet().isEmpty()){
            return R.fail("有礼包包含此通用卡券，无法删除");
        }
        return toAjax(netbarCouponService.deleteWithValidByIds(List.of(couponIds), true));
    }

    @GetMapping("/listNameAndId")
    public R<List<NetbarCouponVo>> listNameAndId() {
        return R.ok(netbarCouponService.listCouponNameAndId());
    }

    @SaCheckPermission("netbar:coupon:use")
    @Log(title = "通用卡券-使用", businessType = BusinessType.UPDATE)
    @PostMapping("/use")
    public R<?> use(@RequestBody NetbarCouponMemberBo bo) {
        R<?> r = couponService.useCoupon(bo);
        memberService.refreshMemberRedis(bo.getTenantId(), bo.getMemberId());
        return r;
    }

}
